▍ humdrum codex / soft
1.1 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { withErrors, ok, apiError, parseJson } from "@/lib/api";
import { requireUser } from "@/lib/auth";
import { UpdateSource } from "@/lib/schemas/source";
import { getSource, updateSource } from "@/lib/store";
import { currentState, ensureFresh } from "@/lib/refresh";

// GET /api/sources/:id — cached state; ?fresh=1 refreshes if stale first.
export const GET = withErrors(async (req, { params }) => {
  await requireUser(req);
  const { id } = await params;
  const source = getSource(id);
  if (!source) return apiError("not_found", "Source not found", 404);

  const fresh = new URL(req.url).searchParams.get("fresh") === "1";
  const state = fresh ? await ensureFresh(source) : currentState(source);
  return ok({ source: state });
});

// PATCH /api/sources/:id — update label / enabled / config / refreshSeconds / position.
export const PATCH = withErrors(async (req, { params }) => {
  await requireUser(req);
  const { id } = await params;
  if (!getSource(id)) return apiError("not_found", "Source not found", 404);
  const patch = await parseJson(req, UpdateSource);
  const source = updateSource(id, patch);
  return ok({ source });
});